package org.wzl.todolisthelper.convert;

import org.mapstruct.BeforeMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.wzl.todolisthelper.domain.PeriodicTask;
import org.wzl.todolisthelper.dto.AddPeriodicTaskDTO;
import org.wzl.todolisthelper.enumerate.DelFlagEnum;
import org.wzl.todolisthelper.enumerate.TaskCardTagEnum;
import org.wzl.todolisthelper.enumerate.TaskDefinitionEnum;
import org.wzl.todolisthelper.utils.IdCommonGen;
import org.wzl.todolisthelper.utils.JsonWrapperUtil;
import org.wzl.todolisthelper.utils.UserUtil;
import org.wzl.todolisthelper.vo.IndexTaskCardVO;
import org.wzl.todolisthelper.vo.PeriodicTaskDetailVO;
import org.wzl.todolisthelper.vo.TaskCardTagVO;

import java.util.ArrayList;
import java.util.List;

@Mapper(componentModel = "spring")
public interface PeriodicTaskConvert {

    @Mapping(ignore = true, target = "periodicTaskDetail", source = "periodicTaskDetail")
    PeriodicTask toDO(AddPeriodicTaskDTO addPeriodicTaskDTO);
    @BeforeMapping
    default void beforeMapping(AddPeriodicTaskDTO addPeriodicTaskDTO, @MappingTarget PeriodicTask periodicTask) {
        String detailString = "";
        if (addPeriodicTaskDTO.getRepetitionCycle().equals("day")) {
            detailString = JsonWrapperUtil.toJson(addPeriodicTaskDTO.getPeriodicTaskOfDay());
        }
        if (addPeriodicTaskDTO.getRepetitionCycle().equals("week")) {
            detailString = JsonWrapperUtil.toJson(addPeriodicTaskDTO.getPeriodicTaskOfWeek());
        }
        if (addPeriodicTaskDTO.getRepetitionCycle().equals("month")) {
            detailString = JsonWrapperUtil.toJson(addPeriodicTaskDTO.getPeriodicTaskOfMonth());
        }
        periodicTask.setPeriodicTaskDetail(detailString);
        periodicTask.setCreateUserId(UserUtil.getCurrentUserId());
        periodicTask.setId(IdCommonGen.generateId());
        periodicTask.setDelFlag(DelFlagEnum.NOT_DEL.getCode());
    }

    IndexTaskCardVO toVO(PeriodicTask periodicTask);
    @BeforeMapping
    default void beforeMapping(PeriodicTask periodicTask, @MappingTarget IndexTaskCardVO indexTaskCardVO) {
        List<TaskCardTagVO> tags = new ArrayList<>();
        tags.add(TaskCardTagEnum.PERIODIC_TASK.getTaskCardTagVO());
        indexTaskCardVO.setTags(tags);

        indexTaskCardVO.setTaskStartDate(periodicTask.getCreateDate());

        indexTaskCardVO.setTaskType(TaskDefinitionEnum.PERIODIC_TASK.getCode());
    }

    PeriodicTaskDetailVO toDetailVO(PeriodicTask periodicTask);
}
